package cn.edu.cqytxy.demo2;

/**
 * @author yinjw
 */
public class LockTest {

    static Object yellow = new Object();
    static Object red = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                lock1();
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                lock2();
            }
        }).start();
    }

    private static void lock1() {
        synchronized (yellow) {
            System.out.println("lock2拿到了yellow");

            try {
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
            synchronized (red){
                System.out.println("lock2拿到了red");
            }
        }
    }

    private static void lock2() {
        synchronized (red) {
            System.out.println("lock1拿到了red");

            synchronized (yellow){
                System.out.println("lock1拿到了yellow");
            }
        }
    }
}
